package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/business/productCategory")
@RestController
public class ProductCategoryController {

    @Resource
    private ProductCategoryService productCategoryService;

    //加载分类数据
    @GetMapping("/categoryTree")
    public ResponseBean categoryTree(Integer pageNum,Integer pageSize){

        Map categoryTreeMap = productCategoryService.productCategoryTreeNodeVOS(pageNum,pageSize);
        return ResponseBean.success(categoryTreeMap);
    }

    //加载父级分类
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> productCategoryVOList = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(productCategoryVOList);
    }

    //添加分类
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO){
        productCategoryService.add(productCategoryVO);

        return ResponseBean.success();
    }

    //查询修改对象
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductCategoryVO productCategoryVO = productCategoryService.edit(id);
        return ResponseBean.success(productCategoryVO);
    }

    //更新对象数据（修改）
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody ProductCategoryVO productCategoryVO){
        productCategoryService.update(id,productCategoryVO);

        return ResponseBean.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean deleteById(@PathVariable Long id){
        productCategoryService.deleteById(id);

        return ResponseBean.success();
    }

}
